package best.driver.android.Service;

import android.annotation.SuppressLint;
import android.app.IntentService;
import android.content.Intent;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.widget.Toast;
import best.driver.android.Utils.Logger;
import best.driver.android.manager.UpgradeManager;

public class BestService extends IntentService {

    public static final String ACTION_UPLOAD_CALL_LOG = "cn.best.android.ACTION_UPLOAD_CALL_LOG";

    public static final String ACTION_INIT_DATA_STORAGE = "cn.best.android.ACTION_INIT_DATA_STORAGE";

    public static final String ACTION_UPDATE_APP_SPLASH = "cn.best.android.ACTION_UPDATE_APP_SPLASH";

    public static final String ACTION_SAVE_CALL_LOG = "cn.best.android.ACTION_SAVE_CALL_LOG";

    private boolean cancelUpdate = false;

    private static final int MSG_SHOW_INFO = 1;

    UpgradeManager upgradeManager;

    @SuppressLint("HandlerLeak")
    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (msg != null) {
                switch (msg.what) {
                case MSG_SHOW_INFO:
                    Toast.makeText(BestService.this, msg.obj.toString(), Toast.LENGTH_SHORT).show();
                    break;
                default:
                    break;
                }
            }
        }
    };

    public BestService() {
        super("BestService");
        setIntentRedelivery(true);
    }

    @Override
    public void onCreate() {
        super.onCreate();
        upgradeManager = UpgradeManager.getInstance(getApplicationContext());
    }

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    protected void onHandleIntent(Intent intent) {
        if (intent == null || intent.getAction() == null) {
            return;
        }
        String gotAction = intent.getAction();
        if (ACTION_UPLOAD_CALL_LOG.equals(gotAction)) {

        } else if (ACTION_SAVE_CALL_LOG.equals(gotAction)) {


        } else if (ACTION_INIT_DATA_STORAGE.equals(gotAction)) {

        }
        Logger.d("intent1", gotAction);
        if (gotAction.startsWith(UpgradeManager.ACTION_HEADER)) {
            upgradeManager.onHandleIntent(intent);
            Logger.d("intent2", gotAction);
        }
    }

}
